package cn.itcast.controller;

import cn.itcast.OrderService;
import cn.itcast.constant.MessageConstant;
import cn.itcast.constant.RedisMessageConstant;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.Result;
import cn.itcast.utils.JuHeUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class orderController {
    @Reference
    OrderService orderService;
    @Autowired
    JedisPool jedisPool;

    /**
     * 体检预约
     *
     * @param map
     * @return
     */
    @RequestMapping("/submit")
    public Result submitOrder(@RequestBody Map map) {
        String telephone = (String) map.get("telephone");
        String telephoneCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        String validateCode = (String) map.get("validateCode");
        if (telephoneCode == null || !telephoneCode.equals(validateCode)) {
//预约失败
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);

        }
//调用体检预约
        Result result = null;
        try {
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
         result=   orderService.order(map);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }


        if (result.isFlag()) {


            try {
                JuHeUtils.getRequest2(telephone,"2222");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return result;
    }
    /**
     * 根据id查询预约信息，包括套餐信息和会员信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Result result =null;

            Map map = orderService.findById4Detail(id);
        return new Result(true,"",map);

    }

}
